package com.yooop.study.singleton.doublechecklock;

import com.yooop.study.singleton.IAction;

/**
 * @author yaopin
 * @since 2025/2/12 21:05
 * Copyright © 2025 Bahamut.NetEase. All rights reserved.
 */
public class Singleton implements IAction {

    // volatile 关键字可以防止 INSTANCE = new Singleton(); 指令重排，多线程情况下某一个线程拿到了一个未初始化的
    private volatile static Singleton INSTANCE;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (INSTANCE == null) {
            synchronized (Singleton.class) {
                if (INSTANCE == null) {
                    INSTANCE = new Singleton();
                }
            }
        }
        return INSTANCE;
    }

    @Override
    public void action() {
        System.out.println("Here is DoubleCheckLock");
    }
}
